/**
 * Copyright 2009 Emory University
 * 
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated  documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense, 
 * and/or sell copies of the Software, and to permit persons to whom the 
 * Software is furnished to do so, subject to the following conditions: The 
 * above copyright notice and this permission notice shall be included in all 
 * copies or substantial portions of the Software. 
 * 
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING 
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */
package com.markgrand.structuredSql.postgresql

import com.markgrand.structuredSql._

/**
 * Append an Update statement with PostgreSQL extensions to a StringBuilder.
 * 
 * @param only
 *      If this is true then the table name will be preceded by the ONLY keyword. 
 * @param tableName
 *      The name of the table to be updated. The name may be a qualified name.
 * @param
 *      An alias to use for the table name elsewhere in the update.
 * @param
 *      The names of the columns to be updated.
 * @param
 *      A where constraint to constrain which rows are updated by the update statement
 * @author Mark Grand
 */
class PostgresqlUpdate protected ( val only: Boolean,
                                   override val tableName:TableName,
                                   override val alias:Option[String],
                                   override val columnNames:Seq[String],
                                   override val values:UpdateValueList,
                                   override val where:Option[WhereConstraint],
                                   val returning:Option[SelectList])
      extends Update(tableName, alias, columnNames, values, where) {
  
  override def append(builder:StringBuilder) {
    if (only) {
      builder.append("UPDATE ONLY ")
      appendUpdateBody(builder)
    } else {
      super.append(builder)
    }
    if (returning.isDefined) {
      builder.append(" RETURNING ")
      returning.get.append(builder)
    }
  }
}

object PostgresqlUpdate {
  def apply(tableName:TableName, columnNames:Seq[String], values:UpdateValueList) = {
    Update(tableName, None, columnNames, values, None)
  }
  def apply(tableName:TableName, alias:String, columnNames:Seq[String], values:UpdateValueList) = {
    Update(tableName, Some(alias), columnNames, values, None)
  }
  def apply(tableName:TableName, columnNames:Seq[String], values:UpdateValueList, where:WhereConstraint) = {
    Update(tableName, None, columnNames, values, Some(where))
  }
  def apply(tableName:TableName, alias:String, columnNames:Seq[String], values:UpdateValueList, where:WhereConstraint) = {
    Update(tableName, Some(alias), columnNames, values, Some(where))
  }
  def apply(only:Boolean, tableName:TableName, columnNames:Seq[String], values:UpdateValueList) = {
    new PostgresqlUpdate(only, tableName, None, columnNames, values, None, None)
  }
  def apply(only:Boolean, tableName:TableName, alias:String, columnNames:Seq[String], values:UpdateValueList) = {
    new PostgresqlUpdate(only, tableName, Some(alias), columnNames, values, None, None)
  }
  def apply(only:Boolean, tableName:TableName, columnNames:Seq[String], values:UpdateValueList, where:WhereConstraint) = {
    new PostgresqlUpdate(only, tableName, None, columnNames, values, Some(where), None)
  }
  def apply(only:Boolean, tableName:TableName, alias:String, columnNames:Seq[String], values:UpdateValueList, where:WhereConstraint) = {
    new PostgresqlUpdate(only, tableName, Some(alias), columnNames, values, Some(where), None)
  }
  def apply(tableName:TableName, columnNames:Seq[String], values:UpdateValueList, returning:SelectList) = {
    new PostgresqlUpdate(false, tableName, None, columnNames, values, None, Some(returning))
  }
  def apply(tableName:TableName, alias:String, columnNames:Seq[String], values:UpdateValueList, returning:SelectList) = {
    new PostgresqlUpdate(false, tableName, Some(alias), columnNames, values, None, Some(returning))
  }
  def apply(tableName:TableName, columnNames:Seq[String], values:UpdateValueList, where:WhereConstraint, returning:SelectList) = {
    new PostgresqlUpdate(false, tableName, None, columnNames, values, Some(where), Some(returning))
  }
  def apply(tableName:TableName, alias:String, columnNames:Seq[String], values:UpdateValueList, where:WhereConstraint, returning:SelectList) = {
    new PostgresqlUpdate(false, tableName, Some(alias), columnNames, values, Some(where), Some(returning))
  }
  def apply(only:Boolean, tableName:TableName, columnNames:Seq[String], values:UpdateValueList, returning:SelectList) = {
    new PostgresqlUpdate(only, tableName, None, columnNames, values, None, Some(returning))
  }
  def apply(only:Boolean, tableName:TableName, alias:String, columnNames:Seq[String], values:UpdateValueList, returning:SelectList) = {
    new PostgresqlUpdate(only, tableName, Some(alias), columnNames, values, None, Some(returning))
  }
  def apply(only:Boolean, tableName:TableName, columnNames:Seq[String], values:UpdateValueList, where:WhereConstraint, returning:SelectList) = {
    new PostgresqlUpdate(only, tableName, None, columnNames, values, Some(where), Some(returning))
  }
  def apply(only: Boolean, tableName:TableName, alias:String, columnNames:Seq[String], 
            values:UpdateValueList, where:WhereConstraint, returning:SelectList) = {
    new PostgresqlUpdate(only, tableName, Some(alias), columnNames, values, Some(where), Some(returning))
  }
  def apply(only: Boolean, tableName: TableName, alias: Option[String], columnNames:Seq[String], values:UpdateValueList, where:Option[WhereConstraint], returning:Option[SelectList]) = {
    new PostgresqlUpdate(only, tableName, alias, columnNames, values, where, returning)
  }
}
